﻿using fuzhu;
using LH.Model.ST;
using LH.U8API.Bus;
using NetTaste;
using Org.BouncyCastle.Bcpg.OpenPgp;
using SqlSuarEntity;
using SqlSugar;
using System.Collections.Generic;
using System.Data;
using U8COWeb;
using XXX.Bo;
using XXX.Common;
using XXX.Models;
using XXX.Models.WMS;
using static System.Runtime.CompilerServices.RuntimeHelpers;
using static XXX.Models.WMS.ScanBarcode;

namespace Yonyou.U8.BO
{
    //扫描条码的
    public class ScanBarcodeVM : ScanBarcode
    {
        //当前的数据库连接字符串
        public string conStr { get; set; }
        public string cacc { get; set; }
        public string xmlConfig { get; set; }
        public string cDatabase = "";
        /// <summary>
        /// 供应商名称
        /// </summary>
        public string cVenName { get; set; }
        //仓库名称
        public string cWhName { get; set; }

        //存储的关联id
        public string cParentid { get; set; }
        public ScanBarcodeVM()
        {
            //conStr
            conStr = CanShu.conStr;
            cacc = CanShu.cZT;
            //初始化定义xmlconfig
            xmlConfig = XmlOperation.GetConfigXML();
        }
        public void ClearConstr()
        {
            conStr = "";
            xmlConfig = "";
        }
      
        /// <summary>
        /// 根据发货单生成出库单
        /// </summary>
        /// <param name="iid"></param>
        /// <returns></returns>
        public RetModel2 ST_SADispToRd32()
        {
            try
            {
                /*
                 *1、保存原始报文数据到ZDY_RDRECORDS_ORI表
                 *2、数据合并，构建入库单实体
                 *3、生成入库单（入库单前再次校验下库存状态）
                 *4、更新条码状态
                 */
                if (itemList.Count == 0)
                {
                    return DBService.ReErrObject("没有数据，无法保存");
                }

                //保存条码原始数据
                RetModel2 retinfo = SaveOriInfo();
                if (retinfo.code != 200)
                {
                    throw new Exception("保存出错：" + retinfo.message);
                }
                else
                {
                    cParentid = DbHelper.GetDbString(retinfo.data);
                }
                //取条码汇总数据
                List<ScanBarcodes> lstnew = GetSumList();
                //构建U8的实体 
                RdRecord32VM vm = GetRd32Entity("发货单", cParentid, lstnew);
                vm.bWMS = true;

                string cRe = vm.GetRd32Info();
                if (cRe != "ok")
                {
                    throw new Exception(cRe);
                }
                RetModel2 ret2 = vm.AddVerify();
                if (ret2.code != 200)
                {
                    return ret2;
                }
                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    //更新wms_rdrecords_ori的id和ccode 
                    string sql = String.Format(@"update a set  irdsid = b.autoid,cU8Code='{2}'  from ZDY_RDRECORDS_ori a,rdrecords32 b where a.cinvcode=b.cinvcode
                and a.parentid = '{0}' and b.id ='{1}' ", cParentid, vm.head.id, DbHelper.GetDbString(ret2.data));
                    db.Ado.ExecuteCommand(sql);

                    sql = string.Format("update rdrecord32 set chandler='{1}' WHERE id='{0}'", vm.head.id, cUserName);
                    db.Ado.ExecuteCommand(sql);

                    //删除临时存储
                    ZDY_RDRECORDS_TMPVM zDY_RDRECORDS_TMPVM = new ZDY_RDRECORDS_TMPVM();
                    zDY_RDRECORDS_TMPVM.ST_Rdrecord_Tmp_Del(cCode + "/" + clientid, cType);

                    //更细条码的状态
                    WMS_BARCODEVM wMS_BARCODEVM = new WMS_BARCODEVM();
                    wMS_BARCODEVM.ChangeBarcodestate(cParentid, "已出库");

                    return DBService.ReOKObject("U8单据完成" + DbHelper.GetDbString(ret2.data));
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }

        }


        

        /// <summary>
        /// 货位调整单
        /// </summary>  
        public RetModel2 ST_AdjustPVouch()
        {
            try
            {
                /*
                 *1、保存原始报文数据到ZDY_RDRECORDS_ORI表
                 *2、数据合并，构建入库单实体
                 *3、生成入库单（入库单前再次校验下库存状态）
                 *4、更新条码状态
                 */
                if (itemList.Count == 0)
                {
                    return DBService.ReErrObject("没有数据，无法保存");
                }

                //保存条码原始数据
                RetModel2 retinfo = SaveOriInfo();
                if (retinfo.code != 200)
                {
                    throw new Exception("保存出错：" + retinfo.message);
                }
                else
                {
                    cParentid = DbHelper.GetDbString(retinfo.data);
                }
                //取条码汇总数据
                List<ScanBarcodes> lstnew = GetSumList();
                string cSource = "库存";

                //构建U8的实体 
                AdjustPVouchVM vm = GetAdjustPEntity(cSource, cParentid, lstnew);
                vm.bWMS = true;

                string cRe = vm.GetRdInfo();
                if (cRe != "ok")
                {
                    throw new Exception(cRe);
                }
                RetModel2 ret2 = vm.AddVerify();
                if (ret2.code != 200)
                {
                    return ret2;
                }
                using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
                {
                    ConnectionString = conStr,
                    DbType = SqlSugar.DbType.SqlServer,
                    IsAutoCloseConnection = true
                }))
                {
                    //更新wms_rdrecords_ori的id和ccode 
                    string sql = String.Format(@"update a set  irdsid = b.autoid,cU8Code='{2}'  from ZDY_RDRECORDS_ori a,AdjustPVouchs b where a.cinvcode=b.cinvcode
                and a.parentid = '{0}' and b.id ='{1}' ", cParentid, vm.head.Id, DbHelper.GetDbString(ret2.data));
                    db.Ado.ExecuteCommand(sql);

                    sql = string.Format("update AdjustPVouch set cHandler='{1}' WHERE id='{0}'", vm.head.Id, cUserName);
                    db.Ado.ExecuteCommand(sql);
                  
                    //更细条码的货位
                    WMS_BARCODEVM wMS_BARCODEVM = new WMS_BARCODEVM();

                    wMS_BARCODEVM.ChangeBarcodeWhcodePos(cParentid);

                    return DBService.ReOKObject("U8单据完成" + DbHelper.GetDbString(ret2.data));
                }

            }
            catch (Exception ex)
            {
                return DBService.ReErrObject(ex.Message);
            }

        }

      

        

      

     
        /// <summary>
        /// 拼接采购入库单实体
        /// </summary>
        ///  <param name="cSource">单据来源，采购到货单，采购订单</param>
        /// <param name="cParentid"></param>
        /// <param name="lstnew"></param>
        /// <returns></returns>
        private RdRecord01VM GetRd01Entity(string cSource,string cParentid,List<ScanBarcodes> lstnew)
        {
            //插入U8
            LH.Model.ST.ST_Rdrecord01.Head rdHead = new LH.Model.ST.ST_Rdrecord01.Head();
            //部门、人员，供应商等，根据表体的订单id取值 
            rdHead.cvouchtype = "01";
            rdHead.crdcode = CanShu.Rd01;
            rdHead.cbustype = "普通采购";
            rdHead.csource = cSource;
            rdHead.cwhcode = cWhCode;
            rdHead.ddate = dDate.ToString("yyyy-MM-dd");
            rdHead.cmaker = cUserName;
            //rdHead.chandler = model.verifier;
            //rdHead.vt_id = "27";
            //rdHead.itaxrate = "13.00";
            //rdHead.iexchrate = "1";
            //rdHead.cexch_name = "人民币";
            rdHead.idiscounttaxtype = "0";
            rdHead.bredvouch = "0";
            //rdHead.cdefine1 = "顾潇";
            rdHead.brdflag = "1";
            //rdHead.cpersoncode = "00043";
            //rdHead.cordercode = "0000000052";
            //rdHead.ipurorderid = "1000000054";
            rdHead.iPrintCount = "0";
            rdHead.ccode = WMSPara.rd01Code;   //为了重号自动重取
            rdHead.cdefine10 = cParentid;   //入库单号写自定义项1
                                            
            List<LH.Model.ST.ST_Rdrecord01.Body> bodies = new List<LH.Model.ST.ST_Rdrecord01.Body>();

            for (int i = 0; i < lstnew.Count; i++)
            {
                LH.Model.ST.ST_Rdrecord01.Body body = new LH.Model.ST.ST_Rdrecord01.Body();
                body.cinvcode = lstnew[i].cInvCode;
                body.cbatch = lstnew[i].cBatch;
                body.cposition = lstnew[i].cPosCode;
                //每箱的汇总数加上
                //body.cdefine24 = DbHelper.GetDbString(dtsum.Rows[i]["A"]);
                //body.cposition = model.materialList[i].cposcode;
                if(cSource=="采购到货单")
                {
                    body.iarrsid = DbHelper.GetDbString(lstnew[i].autoid);
                }
               else if (cSource=="采购订单")
                {
                    body.iposid = DbHelper.GetDbString(lstnew[i].autoid);
                }
                //body.dmadedate = model.materialList[i].manufactureDate;
                body.iquantity = DbHelper.GetDbString(lstnew[i].iQuantity); ;
                //body.inum = model.materialList[i].count;
                body.editprop = "A";
                body.dmadedate = DbHelper.GetDbString(lstnew[i].dMDate);
                //写在失效日期上
                body.dvdate = DbHelper.GetDbString(lstnew[i].dVDate);
                body.cfree1 = lstnew[i].cFree1;
                body.cfree2 = lstnew[i].cFree2;
                body.cfree3 = lstnew[i].cFree3;
                body.cfree4 = lstnew[i].cFree4;
                body.irowno = (i + 1).ToString();


                bodies.Add(body);
            }

            //LH.Model.ST.Rdrecord01 entity = new LH.Model.ST.Rdrecord01(rdHead, bodies);
            RdRecord01VM vM = new RdRecord01VM(rdHead, bodies);
            return vM;
         
        }

        /// <summary>
        /// 拼接销售出库实体
        /// </summary>
        ///  <param name="cSource">单据来源，发货单</param>
        /// <param name="cParentid"></param>
        /// <param name="lstnew"></param>
        /// <returns></returns>
        private RdRecord32VM GetRd32Entity(string cSource, string cParentid, List<ScanBarcodes> lstnew)
        {

           
             
            //插入U8
            LH.Model.ST.ST_Rdrecord32.Head rdHead = new LH.Model.ST.ST_Rdrecord32.Head();
            //部门、人员，供应商等，根据表体的订单id取值 
            rdHead.cvouchtype = "32";
            rdHead.crdcode = CanShu.Rd32;
            rdHead.cbustype = "普通销售";
            rdHead.csource = cSource;
            rdHead.cwhcode = cWhCode;
            rdHead.ddate = dDate.ToString("yyyy-MM-dd");
            rdHead.cmaker = cUserName; 
            //rdHead.cdefine1 = "顾潇";
            rdHead.brdflag = "0";
            //rdHead.cpersoncode = "00043";
            //rdHead.cordercode = "0000000052";
            //rdHead.ipurorderid = "1000000054";
            rdHead.iPrintCount = "0";
            rdHead.ccode = WMSPara.rd32Code;   //为了重号自动重取
            rdHead.cdefine10 = cParentid;   //入库单号写自定义项1

            List<LH.Model.ST.ST_Rdrecord32.Body> bodies = new List<LH.Model.ST.ST_Rdrecord32.Body>();

            for (int i = 0; i < lstnew.Count; i++)
            {
                LH.Model.ST.ST_Rdrecord32.Body body = new LH.Model.ST.ST_Rdrecord32.Body();
                body.cinvcode = lstnew[i].cInvCode;
                body.cbatch = lstnew[i].cBatch;
                body.cposition = lstnew[i].cPosCode;
                //每箱的汇总数加上
                //body.cdefine24 = DbHelper.GetDbString(dtsum.Rows[i]["A"]);
                //body.cposition = model.materialList[i].cposcode;
                if (cSource == "发货单")
                {
                    body.idlsid = DbHelper.GetDbString(lstnew[i].autoid);
                }
                
                //body.dmadedate = model.materialList[i].manufactureDate;
                body.iquantity = DbHelper.GetDbString(lstnew[i].iQuantity); ;
                //body.inum = model.materialList[i].count;
                body.editprop = "A";
                body.dmadedate = DbHelper.GetDbString(lstnew[i].dMDate);
                body.dvdate = DbHelper.GetDbString(lstnew[i].dVDate);
                body.cfree1 = lstnew[i].cFree1;
                body.cfree2 = lstnew[i].cFree2;
                body.cfree3 = lstnew[i].cFree3;
                body.cfree4 = lstnew[i].cFree4;
               
                body.irowno = (i + 1).ToString(); 
                bodies.Add(body);
            }

            //LH.Model.ST.Rdrecord32 entity = new LH.Model.ST.Rdrecord32(rdHead, bodies);
            RdRecord32VM vM = new RdRecord32VM(rdHead, bodies);
            return vM;

        }
        /// <summary>
        /// 拼接调拨申请单实体
        /// </summary>
        ///  <param name="cSource">单据来源，发货单</param>
        /// <param name="cParentid"></param>
        /// <param name="lstnew"></param>
        /// <returns></returns>
        private TransVouchVM GetTransVouchEntity(string cSource, string cParentid, List<ScanBarcodes> lstnew)
        {

            LH.Model.ST.ST_TransVouch.Head rdHead = new LH.Model.ST.ST_TransVouch.Head();
            rdHead.bWireless = "1";
            rdHead.csource = "1";
            rdHead.itransflag = "正向";
            rdHead.dTVDate = dDate.ToString("yyyy-MM-dd");
            rdHead.cIWhCode = cWhCodeIn;
            rdHead.cOWhCode = cWhCode;
            rdHead.cORdCode = CanShu.RdTV09;
            rdHead.cIRdCode = CanShu.RdTV08;
            rdHead.cMaker = cUserName;
            //rdHead.cVerifyPerson = scanBarcodes.cUserName;
            rdHead.cTVCode = WMSPara.rdTVCode;
            rdHead.iPrintCount = "0";
            rdHead.cDefine10 = cParentid;   //入库单号写自定义项10
            rdHead.cTranRequestCode = cCode;   //来源单号
                                                            //部门、人员，供应商等，根据表体的订单id取值
            rdHead.VT_ID = "89";

             
            List<LH.Model.ST.ST_TransVouch.Body> bodies = new List<LH.Model.ST.ST_TransVouch.Body>();

            for (int i = 0; i < lstnew.Count; i++)
            {
                LH.Model.ST.ST_TransVouch.Body body = new LH.Model.ST.ST_TransVouch.Body();
                body.cInvCode = lstnew[i].cInvCode;
                body.cTVBatch = lstnew[i].cBatch;
                body.coutposcode = lstnew[i].cPosCode;
                body.cinposcode = lstnew[i].cPosCodeIn;
                
                if (cSource == "调拨申请调拨")
                {
                    body.iTRIds = DbHelper.GetDbString(lstnew[i].autoid);
                }

                //body.dmadedate = model.materialList[i].manufactureDate;
                body.iTVQuantity = DbHelper.GetDbString(lstnew[i].iQuantity); ;
                //body.inum = model.materialList[i].count;
                body.editprop = "A";
                body.dMadeDate = DbHelper.GetDbString(lstnew[i].dMDate);
                body.dDisDate = DbHelper.GetDbString(lstnew[i].dVDate);
                 body.cFree1 = lstnew[i].cFree1;
                body.cFree2 = lstnew[i].cFree2;
                body.cFree3 = lstnew[i].cFree3;
                body.cFree4 = lstnew[i].cFree4;
                body.irowno = (i + 1).ToString();
                bodies.Add(body);
            }

            //LH.Model.ST.Rdrecord32 entity = new LH.Model.ST.Rdrecord32(rdHead, bodies);
            TransVouchVM vM = new TransVouchVM(rdHead, bodies);
            return vM;

        }

        /// <summary>
        /// 拼接材料出库单
        /// </summary>
        ///  <param name="cSource">单据来源，领料申请单，生产订单，或者库存</param>
        /// <param name="cParentid"></param>
        /// <param name="lstnew"></param>
        /// <returns></returns>
        private RdRecord11VM GetRd11Entity(string cSource, string cParentid, List<ScanBarcodes> lstnew)
        {

            ST_Rdrecord11.Rd11Head rdHead = new ST_Rdrecord11.Rd11Head();
            rdHead.bWireless = "1"; //增加出库速度
            rdHead.cvouchtype = "11";
            //默认，优先取领料申请单上有出库类别
            if (!string.IsNullOrWhiteSpace(cRdCode))
            {
                rdHead.crdcode = cRdCode;
            }
            else
            {
                rdHead.crdcode = CanShu.Rd11MO;
            }
           
            rdHead.cbustype = "领料";
            //单据来源，领料申请单，生产订单，或者库存
            rdHead.csource = cSource;
            rdHead.cwhcode = cWhCode; 
            rdHead.ddate = dDate.ToString("yyyy-MM-dd");  
            rdHead.cmaker =  cUserName;
         
            rdHead.brdflag = "0"; 
            rdHead.iPrintCount = "0";
            rdHead.ccode =WMSPara.rd11Code;
            rdHead.cdefine10 = cParentid;
            //直接领料的时候，备注写在自定义项1
            if(cSource=="库存")
            {
                rdHead.cdepcode = cDepCode; 
                rdHead.cmemo = cDefine1;
            }


            List<LH.Model.ST.ST_Rdrecord11.Rd11Body> bodies = new List<LH.Model.ST.ST_Rdrecord11.Rd11Body>(); 
            for (int i = 0; i < lstnew.Count; i++)
            {
                LH.Model.ST.ST_Rdrecord11.Rd11Body body = new LH.Model.ST.ST_Rdrecord11.Rd11Body();
                body.cinvcode = lstnew[i].cInvCode;
                body.cbatch = lstnew[i].cBatch;
                body.cposition = lstnew[i].cPosCode;
                
                if (cSource == "领料申请单")
                {
                    body.imaids = DbHelper.GetDbString(lstnew[i].autoid);
                }
                else if (cSource == "生产订单")
                {
                    body.impoids = DbHelper.GetDbString(lstnew[i].autoid);
                }
                //body.dmadedate = model.materialList[i].manufactureDate;
                body.iquantity = DbHelper.GetDbString(lstnew[i].iQuantity); ;
                //body.inum = model.materialList[i].count;
                body.editprop = "A";
                body.dmadedate = DbHelper.GetDbString(lstnew[i].dMDate);
                body.dvdate = DbHelper.GetDbString(lstnew[i].dVDate);
                body.cfree1 = lstnew[i].cFree1;
                body.cfree2 = lstnew[i].cFree2;
                body.cfree3 = lstnew[i].cFree3;
                body.cfree4 = lstnew[i].cFree4;

                body.irowno = (i + 1).ToString();
                bodies.Add(body);
            } 

            RdRecord11VM vM = new RdRecord11VM(rdHead, bodies);
            return vM;

        }


        /// <summary>
        /// 拼接其他入库单
        /// </summary>
        ///  <param name="cSource">单据来源，库存</param>
        /// <param name="cParentid"></param>
        /// <param name="lstnew"></param>
        /// <returns></returns>
        private RdRecord08VM GetRd08Entity(string cSource, string cParentid, List<ScanBarcodes> lstnew)
        {

            ST_Rdrecord08.Head rdHead = new ST_Rdrecord08.Head();
            rdHead.bWireless = "1"; //增加出库速度
            rdHead.cvouchtype = "08";
            //默认，优先取领料申请单上有出库类别
            if (!string.IsNullOrWhiteSpace(cRdCode))
            {
                rdHead.crdcode = cRdCode;
            }
            else
            {
                rdHead.crdcode = CanShu.Rd08;
            }

            rdHead.cbustype = "其他入库";
            //单据来源，领料申请单，生产订单，或者库存
            rdHead.csource = cSource;
            rdHead.cwhcode = cWhCode;
            rdHead.ddate = dDate.ToString("yyyy-MM-dd");
            rdHead.cmaker = cUserName;

            rdHead.brdflag = "1";
            rdHead.iPrintCount = "0";
            rdHead.ccode = WMSPara.rd08Code;
            rdHead.cdefine10 = cParentid;
            rdHead.cmemo = cDefine1;



            List<LH.Model.ST.ST_Rdrecord08.Body> bodies = new List<LH.Model.ST.ST_Rdrecord08.Body>();
            for (int i = 0; i < lstnew.Count; i++)
            {
                LH.Model.ST.ST_Rdrecord08.Body body = new LH.Model.ST.ST_Rdrecord08.Body();
                body.cinvcode = lstnew[i].cInvCode;
                body.cbatch = lstnew[i].cBatch;
                body.cposition = lstnew[i].cPosCode;

                //body.dmadedate = model.materialList[i].manufactureDate;
                body.iquantity = DbHelper.GetDbString(lstnew[i].iQuantity); ;
                //body.inum = model.materialList[i].count;
                body.editprop = "A";
                body.dmadedate = DbHelper.GetDbString(lstnew[i].dMDate);
                body.dvdate = DbHelper.GetDbString(lstnew[i].dVDate);
                body.cfree1 = lstnew[i].cFree1;
                body.cfree2 = lstnew[i].cFree2;
                body.cfree3 = lstnew[i].cFree3;
                body.cfree4 = lstnew[i].cFree4;

                body.irowno = (i + 1).ToString();
                bodies.Add(body);
            }

            RdRecord08VM vM = new RdRecord08VM(rdHead, bodies);
            return vM;

        }
        /// <summary>
        /// 拼接其他出库单
        /// </summary>
        ///  <param name="cSource">单据来源，库存</param>
        /// <param name="cParentid"></param>
        /// <param name="lstnew"></param>
        /// <returns></returns>
        private RdRecord09VM GetRd09Entity(string cSource, string cParentid, List<ScanBarcodes> lstnew)
        {

            ST_Rdrecord09.Head rdHead = new ST_Rdrecord09.Head();
            rdHead.bWireless = "1"; //增加出库速度
            rdHead.cvouchtype = "09";
            //默认，优先取领料申请单上有出库类别
            if (!string.IsNullOrWhiteSpace(cRdCode))
            {
                rdHead.crdcode = cRdCode;
            }
            else
            {
                rdHead.crdcode = CanShu.Rd09;
            }

            rdHead.cbustype = "其他出库";
            //单据来源，领料申请单，生产订单，或者库存
            rdHead.csource = cSource;
            rdHead.cwhcode = cWhCode;
            rdHead.ddate = dDate.ToString("yyyy-MM-dd");
            rdHead.cmaker = cUserName;

            rdHead.brdflag = "0";
            rdHead.iPrintCount = "0";
            rdHead.ccode = WMSPara.rd09Code;
            rdHead.cdefine10 = cParentid;
            rdHead.cmemo = cDefine1;
           


            List<LH.Model.ST.ST_Rdrecord09.Body> bodies = new List<LH.Model.ST.ST_Rdrecord09.Body>();
            for (int i = 0; i < lstnew.Count; i++)
            {
                LH.Model.ST.ST_Rdrecord09.Body body = new LH.Model.ST.ST_Rdrecord09.Body();
                body.cinvcode = lstnew[i].cInvCode;
                body.cbatch = lstnew[i].cBatch;
                body.cposition = lstnew[i].cPosCode;
 
                //body.dmadedate = model.materialList[i].manufactureDate;
                body.iquantity = DbHelper.GetDbString(lstnew[i].iQuantity); ;
                //body.inum = model.materialList[i].count;
                body.editprop = "A";
                body.dmadedate = DbHelper.GetDbString(lstnew[i].dMDate);
                body.dvdate = DbHelper.GetDbString(lstnew[i].dVDate);
                body.cfree1 = lstnew[i].cFree1;
                body.cfree2 = lstnew[i].cFree2;
                body.cfree3 = lstnew[i].cFree3;
                body.cfree4 = lstnew[i].cFree4;

                body.irowno = (i + 1).ToString();
                bodies.Add(body);
            }

            RdRecord09VM vM = new RdRecord09VM(rdHead, bodies);
            return vM;

        }

        /// <summary>
        /// 拼接货位调整单
        /// </summary>
        ///  <param name="cSource">单据来源，库存</param>
        /// <param name="cParentid"></param>
        /// <param name="lstnew"></param>
        /// <returns></returns>
        private AdjustPVouchVM GetAdjustPEntity(string cSource, string cParentid, List<ScanBarcodes> lstnew)
        {

            ST_AdjustPVouch.Head rdHead = new ST_AdjustPVouch.Head();
             

            rdHead.csource = "1";
            rdHead.dDate = dDate.ToString("yyyy-MM-dd");
            rdHead.cVouchCode = WMSPara.rdAdjustPCode;
            //rdHead.cMemo = "testCO";
            rdHead.cMaker = cUserName;
            rdHead.cWhCode = cWhCode;
            //rdHead.chandler = scanBarcodes.cUserName;

            rdHead.iPrintCount = "0";
            //rdHead.dVeriDate = model.ddate;
            //rdHead.cdefine1 = model.purchaseReceiptNo;   //入库单号写自定义项1
            //部门、人员，供应商等，根据表体的订单id取值
            List<ST_AdjustPVouch.Body> bodies = new List<ST_AdjustPVouch.Body>();

            for (int i = 0; i < lstnew.Count; i++)
            {
                ST_AdjustPVouch.Body body = new ST_AdjustPVouch.Body();

                body.cInvCode = lstnew[i].cInvCode;
                body.cBatch = lstnew[i].cBatch;
                body.cAPosCode = lstnew[i].cPosCodeIn;
                body.cBPosCode =lstnew[i].cPosCode;
                //body.dmadedate = model.materialList[i].manufactureDate;
                body.iQuantity = DbHelper.GetDbString(lstnew[i].iQuantity); ;
                //body.inum = model.materialList[i].count;
                body.editprop = "A";
                body.dMadeDate = DbHelper.GetDbString(lstnew[i].dMDate);
                body.dDisDate = DbHelper.GetDbString(lstnew[i].dVDate);
                body.cFree1 = lstnew[i].cFree1;
                body.cFree2 = lstnew[i].cFree2;
                body.cFree3 = lstnew[i].cFree3;
                body.cFree4 = lstnew[i].cFree4;

                body.irowno = (i + 1).ToString();
                bodies.Add(body);

                
            }

             AdjustPVouchVM entity = new AdjustPVouchVM(rdHead, bodies);
           
            return entity;

        }


        /// <summary>
        /// 拼接产成品入库单
        /// </summary>
        ///  <param name="cSource">单据来源，领料申请单，生产订单，或者库存</param>
        /// <param name="cParentid"></param>
        /// <param name="lstnew"></param>
        /// <returns></returns>
        private RdRecord10VM GetRd10Entity(string cSource, string cParentid, List<ScanBarcodes> lstnew)
        { 
            ST_Rdrecord10.Rd10Head rdhead = new ST_Rdrecord10.Rd10Head();
            rdhead.bWireless = "1"; //增加出库速度
            rdhead.cvouchtype = "10";
            //默认，优先取领料申请单上有出库类别
            rdhead.crdcode = CanShu.Rd10;
            rdhead.cbustype = "成品入库";
            //单据来源， 生产订单，或者库存
            rdhead.csource = cSource;
            rdhead.cwhcode = cWhCode;
            rdhead.ddate = dDate.ToString("yyyy-MM-dd");
            rdhead.cmaker = cUserName;
            rdhead.brdflag = "1";
            rdhead.iPrintCount = "0";
            rdhead.ccode = WMSPara.rd10Code;
            rdhead.cdefine10 = cParentid;




            List<ST_Rdrecord10.Rd10Body> bodies = new List<ST_Rdrecord10.Rd10Body>();
            for (int i = 0; i < lstnew.Count; i++)
            {
                LH.Model.ST.ST_Rdrecord10.Rd10Body body = new LH.Model.ST.ST_Rdrecord10.Rd10Body();
                body.cinvcode = lstnew[i].cInvCode;
                body.cbatch = lstnew[i].cBatch;
                body.cposition = lstnew[i].cPosCode;
                 
                if  (cSource == "生产订单")
                {
                    body.impoids = DbHelper.GetDbString(lstnew[i].autoid);
                }
                //body.dmadedate = model.materialList[i].manufactureDate;
                body.iquantity = DbHelper.GetDbString(lstnew[i].iQuantity); ;
                //body.inum = model.materialList[i].count;
                body.editprop = "A";
                body.dmadedate = DbHelper.GetDbString(lstnew[i].dMDate);
                body.dvdate = DbHelper.GetDbString(lstnew[i].dVDate);
                body.cfree1 = lstnew[i].cFree1;
                body.cfree2 = lstnew[i].cFree2;
                body.cfree3 = lstnew[i].cFree3;
                body.cfree4 = lstnew[i].cFree4;

                body.irowno = (i + 1).ToString();
                bodies.Add(body);
            }

            RdRecord10VM vM = new RdRecord10VM(rdhead, bodies);
            return vM;

        }
        /// <summary>
        /// 入库申请保存
        /// </summary>
        /// <returns></returns>
        private RetModel2 RuKuSaveOriInfo()
        {
            //插入标记
            string cParentid = System.Guid.NewGuid().ToString();
            List<ZDY_RuKuApp> lst = new List<ZDY_RuKuApp>();
            for (int i = 0; i < itemList.Count; i++)
            {
                if (DbHelper.GetDbInt(itemList[i].iQuantity) != 0)
                {
                    ZDY_RuKuApp rd = new ZDY_RuKuApp();
                    rd.PARENTID = cParentid;
                    rd.cSourceCode = cCode;
                    rd.cInvCode = itemList[i].cInvCode;
                    rd.cBatch = itemList[i].cBatch;
                    rd.iQuantity = itemList[i].iQuantity;
                    rd.cType = cType;
                    rd.cMaker = cUserName;
                    rd.dDate = dDate;
                    rd.cWhCode = cWhCode;
                    rd.cPosCode = itemList[i].cPosCode;
                    rd.cPosCode2 = itemList[i].cPosCodeIn;
                    //rd.cPosName = cPosName;
                    rd.cCusCode = cCusCode;
                    rd.cVenCode = cVenCode;
                    rd.iSourceId = itemList[i].autoid;
                    rd.cBarcodeno = itemList[i].cBarcodeno;
                    rd.dMaketime = DateTime.Now;
                    rd.cDefine1 = itemList[i].cBDefine1;
                    lst.Add(rd);
                }
            }
            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {
                var cnt = db.Insertable(lst) //插入1层 Root->Books
     .ExecuteCommand();
                if (cnt == 0)
                {
                    return DBService.ReErrObject("未保存成功，请重试");
                }
                else
                {
                    return DBService.ReOKObject(cParentid);
                }
            }
        }

        /// <summary>
        /// 保存原始信息
        /// </summary>
        /// <returns></returns>
        private RetModel2 SaveOriInfo()
        {
            //插入标记
            string cParentid = System.Guid.NewGuid().ToString();
            List<ZDY_RDRECORDS_ORI> lst = new List<ZDY_RDRECORDS_ORI>();
            for (int i = 0; i < itemList.Count; i++)
            {
                if (DbHelper.GetDbInt(itemList[i].iQuantity) != 0)
                {
                    ZDY_RDRECORDS_ORI rd = new ZDY_RDRECORDS_ORI();
                    rd.PARENTID = cParentid;
                    rd.cSourceCode = cCode;
                    rd.cInvCode = itemList[i].cInvCode;
                    rd.cBatch = itemList[i].cBatch;
                    rd.iQuantity = itemList[i].iQuantity;
                    rd.cType = cType;
                    rd.cMaker = cUserName;
                    rd.dDate = dDate;
                    rd.cWhCode = cWhCode;
                    rd.cPosCode = itemList[i].cPosCode;
                    rd.cPosCode2 = itemList[i].cPosCodeIn;
                    //rd.cPosName = cPosName;
                    rd.cCusCode = cCusCode;
                    rd.cVenCode = cVenCode;
                    rd.iSourceId = itemList[i].autoid;
                    rd.cBarcodeno = itemList[i].cBarcodeno;
                    rd.dMaketime = DateTime.Now;
                    rd.cDefine1 = itemList[i].cBDefine1; 
                    lst.Add(rd);
                }
            }
            using (SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
            {
                ConnectionString = conStr,
                DbType = SqlSugar.DbType.SqlServer,
                IsAutoCloseConnection = true
            }))
            {
                var cnt = db.Insertable(lst) //插入1层 Root->Books
     .ExecuteCommand();
                if (cnt == 0)
                {
                    return DBService.ReErrObject("未保存成功，请重试");
                }
                else
                {
                    return DBService.ReOKObject(cParentid);
                }
            }
        }

        /// <summary>
        /// 汇总明细数据
        /// </summary>
        /// <returns></returns>
        private List<ScanBarcodes> GetSumList()
        {
            List<ScanBarcodes> lstnew = new List<ScanBarcodes>();

            //数据合并处理
            if (WMSPara.BSumQty == "Y")
            {
                // 使用 LINQ 进行汇总
                lstnew = itemList
                    .GroupBy(barcode => new
                    {
                        barcode.autoid,
                        barcode.modid,
                        barcode.cInvCode,
                        barcode.cFree1,
                        barcode.cFree2,
                        barcode.cFree3,
                        barcode.cFree4,
                        barcode.cBatch,
                        barcode.dMDate,
                        barcode.dVDate,
                        barcode.cInvName,
                        barcode.cInvStd,
                        barcode.cComunitName,
                        barcode.cPosCode,
                        barcode.cPosCodeIn
                    })
                    .Select(group => new ScanBarcodes
                    {
                        autoid = group.Key.autoid,
                        modid = group.Key.modid,
                        cInvCode = group.Key.cInvCode,
                        cFree1 = group.Key.cFree1,
                        cFree2 = group.Key.cFree2,
                        cFree3 = group.Key.cFree3,
                        cFree4 = group.Key.cFree4,
                        cBatch = group.Key.cBatch,
                        dMDate = group.Key.dMDate,
                        dVDate = group.Key.dVDate,
                        cInvName = group.Key.cInvName,
                        cInvStd = group.Key.cInvStd,
                        cComunitName = group.Key.cComunitName,
                        cPosCode = group.Key.cPosCode,
                        cPosCodeIn = group.Key.cPosCodeIn,
                        iQuantity = group.Sum(b => b.iQuantity)
                    }).ToList();
            }
            else
            {
                lstnew = itemList;
            }
            return lstnew;
        }
    }
}
